package cn.wolfcode.web.controller;


import cn.wolfcode.domain.CourseOrder;
import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.mapper.CustomerMapper;
import cn.wolfcode.mapper.SystemDictionaryItemMapper;
import cn.wolfcode.qo.CourseOrderQuery;
import cn.wolfcode.service.ICourseOrderService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/courseOrder")
public class CourseOrderController {

    @Autowired
    private ICourseOrderService courseOrderService;
    @Autowired
    private SystemDictionaryItemMapper systemDictionaryItemMapper;
    @Autowired
    private CustomerMapper customerMapper;


    @RequiresPermissions(value = {"courseOrder:list","课程订单管理页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CourseOrderQuery qo){
        //根据parentId查询学科
        List<SystemDictionaryItem> courses = systemDictionaryItemMapper.selectByPId(qo.getParentId());
        model.addAttribute("courses",courses);
        //客户
        List<Customer> customers = customerMapper.selectAll();
        model.addAttribute("customers",customers);
        //课程
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemMapper.selectByPId(4L);
        model.addAttribute("systemDictionaryItems",systemDictionaryItems);
        PageInfo<CourseOrder> pageInfo = courseOrderService.query(qo);
        model.addAttribute("pageResult", pageInfo);
        return "courseOrder/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"courseOrder:delete","课程订单管理删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            courseOrderService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"courseOrder:saveOrUpdate","课程订单管理新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(CourseOrder courseOrder){
        if (courseOrder.getId() != null) {
            courseOrderService.update(courseOrder);
        }else {
            courseOrderService.save(courseOrder);
        }
        return new JsonResult();
    }
}
